//
//  PlanDao.m
//  SportSocial
//
//  Created by yull on 15/2/3.
//  Copyright (c) 2015年 yull. All rights reserved.
//

#import "PlanDao.h"

@implementation PlanDao

+ (instancetype)shareInstance
{
    static PlanDao *planDao = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        planDao = [[PlanDao alloc] init];
    });
    return planDao;
}

- (void)createTable
{
    [[DBManager shareInstance].dbQueue inDatabase:^(FMDatabase *db) {
        NSMutableArray *sqlArray = [NSMutableArray arrayWithObjects:
                                    @"CREATE TABLE IF NOT EXISTS planInfo ("
                                    "[id] integer NOT NULL PRIMARY KEY AUTOINCREMENT, "
                                    "[icon] varchar,"
                                    "[year] integer,"
                                    "[month] integer,"
                                    "[day] integer,"
                                    "[calorie] integer,"
                                    "[status] integer,"
                                    "[sport_name] varchar,"
                                    "[distance] double,"
                                    "[start_date] double,"
                                    "[end_date] double,"
                                    "[address] varchar,"
                                    "[longitude] double,"
                                    "[latitude] double,"
                                    "[max_speed] double,"
                                    "[avg_speed] double)"
                                    ,nil];
        [db beginTransaction];
        BOOL result = YES;
        for (NSString *sql in sqlArray) {
            result &= [db executeUpdate:sql];
        }
        if (result) {
            result &= [db commit];
        }
        
        if (!result) {
            [db rollback];
        }
    }];
}

- (BOOL)insertPlanInfo:(PlanInfo *)info
{
    __block BOOL result = NO;
    [[DBManager shareInstance].dbQueue inDatabase:^(FMDatabase *db) {
        result = [db executeUpdate:@"insert into planInfo (icon,year,month,day,calorie,status,sport_name,distance,start_date,end_date,address,longitude,latitude,max_speed,avg_speed) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
                  info.icon,
                  [NSNumber numberWithInteger:info.year],
                  [NSNumber numberWithInteger:info.month],
                  [NSNumber numberWithInteger:info.day],
                  [NSNumber numberWithInteger:info.calorie],
                  [NSNumber numberWithInteger:info.status],
                  info.sport_name,
                  [NSNumber numberWithFloat:info.distance],
                  [NSNumber numberWithLongLong:info.start_date],
                  [NSNumber numberWithLongLong:info.end_date],
                  info.address,
                  [NSNumber numberWithFloat:info.longitude],
                  [NSNumber numberWithFloat:info.latitude],
                  [NSNumber numberWithFloat:info.max_speed],
                  [NSNumber numberWithFloat:info.avg_speed]];
        if (result) {
            info.dbId = db.lastInsertRowId;
        }
    }];
    return result;
}

- (BOOL)updatePlanInfo:(PlanInfo *)info
{
    __block BOOL result = NO;
    [[DBManager shareInstance].dbQueue inDatabase:^(FMDatabase *db) {
        result = [db executeUpdate:@"update planInfo set icon=?,year=?,month=?,day=?,calorie=?,status=?,sport_name=?,distance=?,start_date=?,end_date=?,address=?,longitude=?,latitude=?,max_speed=?,avg_speed=? where id=?",
                  info.icon,
                  [NSNumber numberWithInteger:info.year],
                  [NSNumber numberWithInteger:info.month],
                  [NSNumber numberWithInteger:info.day],
                  [NSNumber numberWithInteger:info.calorie],
                  [NSNumber numberWithInteger:info.status],
                  info.sport_name,
                  [NSNumber numberWithFloat:info.distance],
                  [NSNumber numberWithLongLong:info.start_date],
                  [NSNumber numberWithLongLong:info.end_date],
                  info.address,
                  [NSNumber numberWithFloat:info.longitude],
                  [NSNumber numberWithFloat:info.latitude],
                  [NSNumber numberWithFloat:info.max_speed],
                  [NSNumber numberWithFloat:info.avg_speed],
                  [NSNumber numberWithLongLong:info.dbId]];
       
    }];
    return result;
}

- (BOOL)deletePlanInfo:(PlanInfo *)info
{
    __block BOOL result = NO;
    [[DBManager shareInstance].dbQueue inDatabase:^(FMDatabase *db) {
        result = [db executeUpdate:@"delete from planInfo where id=?",[NSNumber numberWithLongLong:info.dbId]];
    }];
    return result;
}

- (NSArray *)selectInfoWithYear:(NSInteger)year Month:(NSInteger)month
{
    __block NSMutableArray *returnArray = [NSMutableArray array];
    [[DBManager shareInstance].dbQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *resultset = [db executeQuery:@"select * from planInfo where year=? and month=?",[NSNumber numberWithInteger:year],[NSNumber numberWithInteger:month]];
        while (resultset.next) {
            [returnArray addObject:[PlanInfo planInfoWithResutlSet:resultset]];
        }
        [resultset close];
    }];
    return returnArray;
}

- (NSArray *)selectInfoWithYear:(NSInteger)year Month:(NSInteger)month Day:(NSInteger)day
{
    __block NSMutableArray *returnArray = [NSMutableArray array];
    [[DBManager shareInstance].dbQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *resultset = [db executeQuery:@"select * from planInfo where year=? && month=? && day=?",[NSNumber numberWithInteger:year],[NSNumber numberWithInteger:month],[NSNumber numberWithInteger:day]];
        while (resultset.next) {
            [returnArray addObject:[PlanInfo planInfoWithResutlSet:resultset]];
        }
        [resultset close];
    }];
    return returnArray;
}
@end
